<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\GameServer\ChannelRepository;
use App\Http\Requests\Admin\CreateChannelRequest;
// use App\Http\Requests\Admin\EditChannelRequest;
use Validator;
use Illuminate\Validation\Rule;

class ChannelController extends Controller
{
    protected $Repo;

    public function __construct(ChannelRepository $repo)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $channels = $this->Repo->getChannels();

        return view('admin.channels.index', compact('channels'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.channels.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateChannelRequest $request)
    {
        $res = $this->Repo->addChannel($request);
        if ($res) {
            $data = ['status' => 0, 'msg' => '操作成功','url' => route('channels.index')];
        } else {
            $data = ['status' => 1, 'msg' => '操作失败'];
        }
        return json_encode($data);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $channel = $this->Repo->getChannel($id);

        return view('admin.channels.edit', compact('channel'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'account' => ['bail','required', Rule::unique('game_server.channels')->ignore($id)],
            'distributer_id' => ['required', Rule::unique('game_server.channels')->ignore($id)],
        ],[
            'account.required' => '账户名称必填',
            'account.unique'   => '账户名已存在',
            'distributer_id.required' => '渠道编号必填',
            'distributer_id.unique' => '渠道编号已存在',
        ])->validate();

        $res = $this->Repo->updateChannel($request, $id);

        if ($res) {
            $data = ['status' => 0, 'msg' => '操作成功', 'url' => route('channels.index')];
        } else {
            $data = ['status' => 1, 'msg' => '操作失败'];
        }

        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $res = $this->Repo->deleteChannel($id);

        if ($res) {
            $data = ['status' => 0, 'msg' => '操作成功', 'url' => route('channels.index')];
        } else {
            $data = ['status' => 1, 'msg' => '操作失败'];
        }

        return json_encode($data);
    }

    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $res = $this->Repo->deleteChannel($ids);
            if ($res) {
                $data = ['status' => 0, 'msg' => '删除成功','url' => route('channels.index')];
            } else {
                $data = ['status' => 2, 'msg' => '删除失败'];
            }
            return json_encode($data);
        }
    }

    public function editStatus(Request $request, $id)
    {
        $res = $this->Repo->editStatus($id, $request->status);

        if ($res) {
            $data = ['status' => 0, 'msg' => '操作成功', 'url' => route('channels.index')];
        } else {
            $data = ['status' => 0, 'msg' => '操作失败'];
        }

        return json_encode($data);
    }
}
